﻿namespace DeltaGroup.WheelOfJeopardy.ChallengeManager
{
    using System.Collections.Generic;

    using DeltaGroup.WheelOfJeopardy.Model;

    public sealed class SynonymComparer : IEqualityComparer<string>
    {
        #region Fields

        private List<Synonym> synonymList;

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="SynonymComparer"/> class.
        /// </summary>
        /// <param name="synonymList">The synonym list.</param>
        public SynonymComparer(List<Synonym> synonymList)
            : this()
        {
            this.synonymList = synonymList;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SynonymComparer"/> class.
        /// </summary>
        private SynonymComparer()
        {
        }

        #endregion Constructors

        #region Methods

        /// <summary>
        /// Equalses the specified x.
        /// </summary>
        /// <param name="x">The x.</param>
        /// <param name="y">The y.</param>
        /// <returns></returns>
        bool IEqualityComparer<string>.Equals(string x, string y)
        {
            bool found = x.Equals(y);

            if (!found)
            {
                foreach (Synonym synonym in synonymList)
                {
                    found = y.Equals(synonym.Alternate);
                    if (found) { break; }
                }
            }

            return found;
        }

        /// <summary>
        /// Returns a hash code for this instance.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <returns>
        /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. 
        /// </returns>
        int IEqualityComparer<string>.GetHashCode(string obj)
        {
            return obj.GetHashCode();
        }

        #endregion Methods
    }
}